﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace Freshbox2._0.UI.Controllers
{
    public class ManagerFeedBackController : BaseController<mfeedback>
    {
        //
        // GET: /ManagerFeedBack/
        protected admin CurrentUer
        {
            get { return Session["CurrentUer"] == null ? null : (admin)Session["CurrentUer"]; }
        }

        public override BLL.BaseBll<mfeedback> bll
        {
            get
            {
                return new ManagerFeedBackBll();
            }

        }
        ManagerBll managerBll = new ManagerBll();
        /// <summary>
        /// 定义类  前台显示
        /// </summary>
        public class ListNoticeModel  //feedbackId  feedbackValue  feedbackTime wechatCode userName wechatName
        {
            public long feedbackId;
            public string feedbackValue;
            public string feedbackTime;
            public int userCode;
            public string userName;
            public string userPhone;
            public int? IsReply;

        }
        /// <summary>
        /// 加载数据列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <param name="model"></param>
        /// <param name="date1"></param>
        /// <param name="date2"></param>
        /// <returns></returns>
        public ActionResult GetNotice(int page, int rows, mfeedback model, string date1, string date2)
        {
            int count;
            var listNoticeModel = new List<ListNoticeModel>();

            //1. 查询出意见表表里的所有的信息                           
            Expression<Func<mfeedback, bool>> condition1 = x => x.managerCode != 0;

            if (string.IsNullOrEmpty(date1) || string.IsNullOrEmpty(date2))
            {
                // date1 date2为空，代表第一次查询 或者是没有选日期的查询，默认选查询所有的数据
                condition1 = x => x.managerCode != 0; ;
            }
            else
            {
                // 1.先筛选出这个时间段的 满足要求的信息 
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
                condition1 = x => x.mfDate >= datetime1 && x.mfDate <= datetime2;
            }
            var listNotice = bll.Search(model, x => true, false, x => x.mfId, rows, page, out count).Where(condition1).Select(x => new { x.mfId, x.managerCode, x.mfValue, x.mfDate, x.mfStatus }).ToList();

            foreach (var oneCode in listNotice)
            {
                //根据订单详情表里的商品编号  进行查询用户表的相关信息     
                var list2 = managerBll.Search(x => x.userid == oneCode.managerCode).Select(x => new { x.RealName, x.userid, x.userPhone }).ToList();
                if (list2.Count != 0)
                {
                    ListNoticeModel salePro = new ListNoticeModel();
                    salePro.feedbackId = oneCode.mfId;
                    salePro.IsReply = oneCode.mfStatus;
                    salePro.userPhone = list2.FirstOrDefault().userPhone;
                    salePro.feedbackValue = oneCode.mfValue;
                    salePro.userCode = list2.FirstOrDefault().userid;
                    salePro.userName = list2.FirstOrDefault().RealName;
                    salePro.feedbackTime = Convert.ToDateTime(oneCode.mfDate).ToString("yyyy-MM-dd HH:mm:ss");
                    listNoticeModel.Add(salePro);
                }
            }

            var listNotice2 = bll.Search(x=>x.IsRead==0).Where(condition1).Select(x => new { x.mfId, x.managerCode, x.mfValue, x.mfDate, x.mfStatus }).ToList();

            //修改   未读已读状态

            foreach (var item in listNotice2)
            {
                mfeedback model1 = new mfeedback();
                model1.mfId = item.mfId;
                model1.managerCode = item.managerCode;
                model1.mfValue = item.mfValue;
                model1.mfDate = item.mfDate;
                model1.mfStatus = item.mfStatus;
                model1.IsRead = 1;//已读  
                if (bll.Modify(model1) > 0)
                {
 
                }
            }

            return Json(new { rows = listNoticeModel, total = count });
        }

        MailContentBll mailContentBll = new MailContentBll();

        MailBll mailBll = new MailBll();

        /// <summary>
        /// 回复单个消息
        /// </summary>
        /// <param name="userCode"></param>
        /// <returns></returns>
        public ActionResult ModifyReply(int mfId, mailcontent contentModel)
        {
            string msg = "";
            int result = 0;
            contentModel.mcTime = DateTime.Now;
            contentModel.mcType = 1;
            //先在 mailContent 表里面添加记录
            if (mailContentBll.Add(contentModel) > 0)
            {
                //然后再mail  表里面添加了记录
                mail mailModel = new mail();
                mailModel.fromManager = 0;
                var list = bll.Search(x => x.mfId == mfId).Select(x => new { x.mfId, x.managerCode ,x.mfDate,x.mfValue}).ToList();
                mailModel.toManager = list.FirstOrDefault().managerCode;
                mailModel.mfDate = DateTime.Now;
                mailModel.mcId = contentModel.mcId;
                mailModel.status = 1;
                mailModel.mfId = mfId;

                if (mailBll.Add(mailModel) > 0)
                {
                    mfeedback feedBackModel = new mfeedback();
                    feedBackModel.mfId = mfId;
                    feedBackModel.managerCode = list.FirstOrDefault().managerCode;
                    feedBackModel.mfValue = list.FirstOrDefault().mfValue;
                    feedBackModel.mfDate = list.FirstOrDefault().mfDate;
                    feedBackModel.mfStatus = 1;
                    //  修改 回复 状态 
                    if (bll.Modify(feedBackModel)>0)
                    {
                        msg = "回复成功";
                        result = 1;
                    }
                }
            }
            return Json(new { state = result, msg = msg });
        }

        /// <summary>
        /// 群发消息   搞定
        /// </summary>
        /// <returns></returns>
        public ActionResult GroupSend(mailcontent contentModel)
        {
            string msg = "";
            int result = 0;
            int jieguo = 1;
            contentModel.mcType = 0;
            contentModel.mcTime = DateTime.Now;
            if (mailContentBll.Add(contentModel) > 0)
            {
                msg = "发送成功";
                result = 1;
                jieguo = 1;
            }
            return Json(new { jieguo = jieguo, state = result, msg = msg });
        }

    }
}


